package com.sc.producer.service.Impl;

import com.sc.producer.entity.Goods;
import com.sc.producer.GoodsRepository;
import com.sc.producer.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class GoodsServiceImpl  implements GoodsService {
    Logger loggerFactory=LoggerFactory.getLogger(GoodsServiceImpl.class);
    @Resource
    private GoodsRepository goodsRepository;
    @Override
    public List<Goods> findAllGoods() {
        return goodsRepository.findAll();
    }

    @Override
    public Goods findGoodsById(Integer id) {
        return goodsRepository.findById(id).get();
    }

    @Override
    public Goods addGoods(Goods goods) {
        return goodsRepository.save(goods);
    }

    @Override
    public Goods updateGoods(Goods goods) {
        return goodsRepository.saveAndFlush(goods);
    }

    @Override
    public boolean deleteGoodsById(Integer id) {
        try {
            goodsRepository.deleteById(id);
            return true;
        }catch (Exception e)
        {
            loggerFactory.info(e.getMessage());
        }
        return false;
    }
}
